
  <template>
  <div class="bf-commont-template">
    <!-- <h3>流程管理</h3>-->

    <div class="clearfix bf-top-button">
      <div class="float-l">
        <el-button @click="add" type="primary">新增</el-button>
      </div>
      <div class="float-r">
        <div class="float-l">
          <el-input placeholder="请输入流程定义名称搜索" prefix-icon="el-icon-search" v-model="searchData" @keyup.enter.native="quickSearch"></el-input>
        </div>
        <div class="float-l padding-l">
          <!-- <el-button @click="showSearch" type="primary">高级筛选</el-button> -->
        </div>
      </div>
    </div>
    <div class="margin-l margin-r">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane :label="'用户'" status="0" name="waitSubmit">
          <bf-table :data="userData" :setting="userListsetting" :rowAction="userRowAction" @editSubmit="editSubmit" @pageChange="pageChange" ref="table"></bf-table>
        </el-tab-pane>
        <el-tab-pane :label="'角色'" status="1" name="vetting">
          <bf-table :data="userData" :setting="roleListsetting" :rowAction="roleRowAction" @editSubmit="editSubmit" @pageChange="pageChange" ref="table"></bf-table>
        </el-tab-pane>
        <el-tab-pane :label="'部门' " status="2" name="vetted">
          <bf-table :data="userData" :setting="orgListsetting" :rowAction="orgRowAction" @editSubmit="editSubmit" @pageChange="pageChange" ref="table"></bf-table>
        </el-tab-pane>
        <el-tab-pane :label="'流程'" status="3" name="stop">
          <bf-table :data="userData" :setting="followListsetting" :rowAction="followRowAction" @editSubmit="editSubmit" @pageChange="pageChange" ref="table"></bf-table>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!--高级搜索 -->
    <bf-table-search :editForm='userAddsetting' :title="'高级搜索'" ref='searchRosterRef' @formSubmit='searchRosterSubmit'></bf-table-search>
    <!-- 用户添加 -->
    <bf-table-edit :editForm='userAddsetting' :title="'添加'" @formSubmit='addSubmit' ref='addUserRef' :editRules="addUserRules"></bf-table-edit>
    <!-- 用户编辑 -->
    <bf-table-edit :editForm='userEditsetting' :title="'编辑'" @formSubmit='editInfo' ref='editUserRef' :editRules="addUserRules"></bf-table-edit>
    <!--用户详情-->
    <bf-table-detail ref="userDetail" :title="'详情'" :editForm="userEditsetting"></bf-table-detail>

    <!-- 角色添加 -->
    <bf-table-edit :editForm='roleAddsetting' :title="'添加'" @formSubmit='addSubmit' ref='addRoleRef' :editRules="addRoleRules"></bf-table-edit>
    <!--角色 编辑 -->
    <bf-table-edit :editForm='roleEditsetting' :title="'编辑'" @formSubmit='editInfo' ref='editRolerRef' :editRules="addRoleRules"></bf-table-edit>
    <!--角色详情-->
    <bf-table-detail ref="roleDetail" :title="'详情'" :editForm="roleDetailsetting"></bf-table-detail>

    <!-- 部门添加 -->
    <bf-table-edit :editForm='orgAddsetting' @formSubmit='addSubmit' :title="'添加'" ref='addOrgRef' :editRules="addOrgRules"></bf-table-edit>
    <!--部门 编辑 -->
    <bf-table-edit :editForm='orgEditsetting' :title="'编辑'" @formSubmit='editInfo' ref='editOrgRef' :editRules="addOrgRules"></bf-table-edit>
    <!--部门详情-->
    <bf-table-detail ref="orgDetail" :title="'详情'" :editForm="orgListsetting"></bf-table-detail>

    <!-- 流程添加 -->
    <bf-table-edit :editForm='followListsetting' @formSubmit='addSubmit' :title="'添加'" ref='addFollowRef' :editRules="addRules"></bf-table-edit>
    <!--流程 编辑 -->
    <bf-table-edit :editForm='followListsetting' :title="'编辑'" @formSubmit='editInfo' ref='editFollowRef' :editRules="addRules"></bf-table-edit>
    <!--流程详情-->
    <bf-table-detail ref="followDetail" :title="'详情'" :editForm="followListsetting"></bf-table-detail>

  </div>
</template>

<script>
import {
  getUserList, getOrgList, getRoleList,
  getfollowList, userAdd, roleAdd, orgAdd,
  updateUser, updateRole, updateOrg,
  updateFollow, deleteOrg, deleteRole, deleteUser
} from './api.js'
import {
  userListsetting, userAddsetting,
  userEditsetting, roleListsetting,
  roleDetailsetting, roleAddsetting,
  orgAddsetting, orgListsetting,
  followListsetting, roleEditsetting,
  orgEditsetting
} from './table.setting.js'
export default {
  data() {
    return {
      searchData: '',
      activeName: 'waitSubmit',
      userData: {
      },
      userRowAction: [// 用户
        {
          label: '查看',
          click: (item) => {
            this.findDetailById(item.scope.row, this.$refs.userDetail.show)
          }
        },
        {
          label: '编辑',
          click: (item) => {
            this.findDetailById(item.scope.row, this.$refs.editUserRef.show)
          }
        },
        {
          label: '删除',
          click: (item) => {
            this.$refs.table.showDelete(item)
          }
        }
      ],
      roleRowAction: [// 角色
        {
          label: '查看',
          click: (item) => {
            this.findDetailById(item.scope.row, this.$refs.roleDetail.show)
          }
        },

        {
          label: '编辑',
          click: (item) => {
            this.findDetailById(item.scope.row, this.$refs.editRolerRef.show)
          }
        },
        {
          label: '删除',
          click: (item) => {
            this.$refs.table.showDelete(item)
          }
        }
      ],
      orgRowAction: [// 部门
        {
          label: '查看',
          click: (item) => {
            this.findDetailById(item.scope.row, this.$refs.orgDetail.show)
          }
        },
        {
          label: '编辑',
          click: (item) => {
            this.findDetailById(item.scope.row, this.$refs.editOrgRef.show)
          }
        },

        {
          label: '删除',
          click: (item) => {
            this.$refs.table.showDelete(item)
          }
        }
      ],
      followRowAction: [//
        {
          label: '查看',
          click: (item) => {
            this.findDetailById(item.scope.row, this.$refs.followDetail.show)
          }
        },
        {
          label: '编辑',
          click: (item) => {
            this.findDetailById(item.scope.row, this.$refs.editFollowRef.show)
          }
        }
      ],
      userListsetting: userListsetting,
      userAddsetting: userAddsetting,
      userEditsetting: userEditsetting,
      roleListsetting: roleListsetting,
      roleAddsetting: roleAddsetting,
      roleDetailsetting: roleDetailsetting,
      orgListsetting: orgListsetting,
      followListsetting: followListsetting,
      roleEditsetting: roleEditsetting,
      orgEditsetting: orgEditsetting,
      orgAddsetting: orgAddsetting,
      addRules: {
        reqName: [
          { required: true, message: '请输入需求名称', trigger: 'blur' },
          { min: 3, max: 50, message: '长度在 3 到 50个字符', trigger: 'blur' }
        ]
      },
      addUserRules: {
        procDefId: [
          { required: true, message: '请选择流程定义', trigger: 'blur' }
        ],
        taskKey: [
          { required: true, message: '请选择任务节点KEY', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: '请选择用户', trigger: 'blur' }
        ]
      },
      addRoleRules: {
        procDefId: [
          { required: true, message: '请选择流程定义', trigger: 'blur' }
        ],
        taskKey: [
          { required: true, message: '请选择任务节点KEY', trigger: 'blur' }
        ]
      },
      addOrgRules: {
        procDefId: [
          { required: true, message: '请选择流程定义', trigger: 'blur' }
        ],
        taskKey: [
          { required: true, message: '请选择任务节点KEY', trigger: 'blur' }
        ],
        orgId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        orgScope: [
          { required: true, message: '请选择是否子机构', trigger: 'blur' }
        ]
      },
      searchParams: { // 搜索条件
        page: 1
      }
    }
  },
  mounted: function() {
    this.searchList()
  },
  methods: {
    searchRosterSubmit(form) { // 高级搜索
      // this.searchList(form)
      // this.$refs.searchRosterRef.hiden()
    },
    findDetailById(form, showCall) { // 详情
      showCall(form)
    },
    searchList() {
      const pageParams = this.searchParams
      const status = this.getActivityStatus()
      if (status === '0') {
        getUserList(pageParams).then((response) => {
          this.userData = response
        })
      }
      if (status === '1') {
        getRoleList(pageParams).then((response) => {
          this.userData = response
        })
      }
      if (status === '2') {
        getOrgList(pageParams).then((response) => {
          this.userData = response
        })
      }
      if (status === '3') {
        getfollowList(pageParams).then((response) => {
          this.userData = response
        })
      }
    },
    editSubmit(params, formType, formList) {
      if (formType === 'delete') {
        this.deleteItem(params.row)
      }
    },
    /**
     * 删除
     */
    deleteItem(form) {
      const status = this.getActivityStatus()
      if (status === '0') {
        deleteUser(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '删除成功!',
              type: 'success'
            })
            this.searchList()
          }
        })
      }
      if (status === '1') {
        deleteRole(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '删除成功!',
              type: 'success'
            })
            this.searchList()
          }
        })
      }
      if (status === '2') {
        deleteOrg(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '删除成功!',
              type: 'success'
            })
            this.searchList()
          }
        })
      }
    },
    pageChange(pageObj) {
      this.searchParams.page = pageObj.page
      this.searchParams.pageSize = pageObj.pageSize
      this.searchList()
    },
    showSearch() {
      this.$refs.searchRosterRef.show()
    },
    add() { // 新增的弹框
      // addUserRef,addRoleRef,addOrgRef,addFollowRef
      const status = this.getActivityStatus()
      if (status === '0') {
        this.$refs.addUserRef.show('')
      }
      if (status === '1') {
        this.$refs.addRoleRef.show('')
      }
      if (status === '2') {
        this.$refs.addOrgRef.show('')
      }
      if (status === '3') {
        this.$message({
          message: '流程无新增功能',
          type: 'error'
        })
        return false
      }
    },
    addSubmit(form) { // 新增保存
      const status = this.getActivityStatus()
      if (status === '0') {
        userAdd(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功!',
              type: 'success'
            })
            this.$refs.addUserRef.hiden()
            this.searchList()
          }
        })
      }
      if (status === '1') {
        // console.log(form)
        roleAdd(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功!',
              type: 'success'
            })
            this.searchList()
            this.$refs.addRoleRef.hiden()
          }
        })
      }
      if (status === '2') {
        orgAdd(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功!',
              type: 'success'
            })
            this.searchList()
            this.$refs.addOrgRef.hiden()
          }
        })
      }
      if (status === '3') {
        this.$message({
          message: '流程无新增保存功能',
          type: 'error'
        })
        return false
      }
    },
    editInfo(form) {
      const status = this.getActivityStatus()
      if (status === '0') {
        updateUser(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功!',
              type: 'success'
            })
            this.$refs.editUserRef.hiden()
            this.searchList()
          }
        })
      }
      if (status === '1') {
        updateRole(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功!',
              type: 'success'
            })
            this.$refs.editRolerRef.hiden()
            this.searchList()
          }
        })
      }
      if (status === '2') {
        updateOrg(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功!',
              type: 'success'
            })
            this.$refs.editOrgRef.hiden()
            this.searchList()
          }
        })
      }
      if (status === '3') {
        updateFollow(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功!',
              type: 'success'
            })
            this.$refs.editFollowRef.hiden()
            this.searchList()
          }
        })
      }
    },
    quickSearch() {
      this.searchParams.page = 1
      if (this.searchData) {
        this.searchParams.procDefName = this.searchData
      } else {
        delete this.searchParams.procDefName
      }

      this.searchList()
    },
    handleClick(tab, event) {
      this.searchParams.page = 1
      this.searchList()
    },
    getActivityStatus() {
      if (this.activeName === 'waitSubmit') {
        return '0';
      }
      if (this.activeName === 'vetting') {
        return '1';
      }
      if (this.activeName === 'vetted') {
        return '2';
      }
      if (this.activeName === 'stop') {
        return '3';
      }
    }
  }
}
</script>

<style scoped>

</style>
